package com.qire.manhua.model.viewModel.impl;

import android.graphics.Typeface;

import com.qire.common.widget.adapter.auto.AutoVariable;
import com.qire.common.widget.adapter.auto.EventReceiver;
import com.qire.common.widget.adapter.auto.SummerPagerAdapter;
import com.qire.common.widget.adapter.auto.annotation.AdapterEntityBind;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleRankBinding;
import com.qire.manhua.databinding.LayoutHomeRecommendModuleRankItemBinding;
import com.qire.manhua.model.entity.HomeModuleEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleRankEntity;
import com.qire.manhua.model.entity.HomeModuleEntity.HomeModuleRankEntity.HomeModuleRankItemEntity;
import com.qire.manhua.model.enumeration.HomeModuleType;

import java.util.ArrayList;

import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.viewpager.widget.PagerAdapter;

/**
 * 首页 -> 推荐 -> 排行榜模块
 * 用于构建组合首页复合布局中的排行榜模块，多复合模块由 {@link HomeModuleType#buildModuleViewModels HomeModuleType} 根据集体类型来构建。
 * @see HomeModuleType#TYPE_RANK
 */
@AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_rank)
public class RecommendModuleRankViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendModuleRankBinding>, EventReceiver {

    private final HomeModuleEntity homeModuleEntity;

    private final ArrayList<ModuleRankPagerViewModel> rankPagerViewModelList = new ArrayList<>();
    private final SummerPagerAdapter pagerAdapter = new SummerPagerAdapter(rankPagerViewModelList);

    private final ModuleRankTabViewModel hotSaleRank = new ModuleRankTabViewModel(0);
    private final ModuleRankTabViewModel newRank = new ModuleRankTabViewModel(1);
    private final ModuleRankTabViewModel freeRank = new ModuleRankTabViewModel(2);

    private int currentItem = 0;

    public RecommendModuleRankViewModel(HomeModuleEntity homeModuleEntity){
        this.homeModuleEntity   = homeModuleEntity;
        initUI();
    }

    @Override
    public void bindVariable(LayoutHomeRecommendModuleRankBinding viewBind) {
        viewBind.setRecommendModuleRankViewModel(this);
    }

    @Override
    public void onReceiver(AutoVariable eventProcess) {
        pagerAdapter.setEventProcess(eventProcess);
    }

    private void initUI() {
        rankPagerViewModelList.clear();
        ArrayList<HomeModuleRankEntity> moduleRankList = homeModuleEntity.moduleRankList();
        for(HomeModuleRankEntity rankEntity : moduleRankList) {
            rankPagerViewModelList.add(new ModuleRankPagerViewModel(0, rankEntity));
            rankPagerViewModelList.add(new ModuleRankPagerViewModel(1, rankEntity));
        }
    }

    public void rankViewPagerOnPageSelected(int position) {
        gotoPagePosition(position);
    }

    private void gotoPagePosition(int position) {
        this.currentItem = position;

        notifyPropertyChanged(BR.currentItem);
        notifyPropertyChanged(BR.hotSaleRank);
        notifyPropertyChanged(BR.newRank);
        notifyPropertyChanged(BR.freeRank);
    }

    @Bindable
    public ModuleRankTabViewModel getHotSaleRank() {
        return hotSaleRank;
    }
    @Bindable
    public ModuleRankTabViewModel getNewRank() {
        return newRank;
    }
    @Bindable
    public ModuleRankTabViewModel getFreeRank() {
        return freeRank;
    }

    @Bindable
    public int getCurrentItem() {
        return currentItem;
    }

    @Bindable
    public PagerAdapter getPagerAdapter(){
        return pagerAdapter;
    }

    private final int[] RankIconRids = {
            R.drawable.app_home_recommend_ranking_icon_1,
            R.drawable.app_home_recommend_ranking_icon_2,
            R.drawable.app_home_recommend_ranking_icon_3,
            R.drawable.app_home_recommend_ranking_icon_4,
            R.drawable.app_home_recommend_ranking_icon_5,
            R.drawable.app_home_recommend_ranking_icon_6
    };

    @AdapterEntityBind(viewRid = R.layout.layout_home_recommend_module_rank_item)
    public class ModuleRankPagerViewModel extends BaseObservable implements AutoVariable<LayoutHomeRecommendModuleRankItemBinding> {

        private final int pageNo;
        private final HomeModuleRankEntity rankEntity;

        public ModuleRankPagerViewModel(int pageNo,HomeModuleRankEntity rankEntity){
            this.pageNo     = pageNo;
            this.rankEntity = rankEntity;
        }

        @Override
        public void bindVariable(LayoutHomeRecommendModuleRankItemBinding viewBind) {
            viewBind.setModuleRankPagerViewModel(this);
        }

        @Bindable
        public HomeModuleRankItemEntity getRankItem1(){
            if(rankEntity.rankInfoList.isEmpty())
                return null;
            return rankEntity.rankInfoList.get(0 + pageNo*3);
        }
        @Bindable
        public HomeModuleRankItemEntity getRankItem2(){
            if(rankEntity.rankInfoList.isEmpty())
                return null;
            return rankEntity.rankInfoList.get(1 + pageNo*3);
        }
        @Bindable
        public HomeModuleRankItemEntity getRankItem3(){
            if(rankEntity.rankInfoList.isEmpty())
                return null;
            return rankEntity.rankInfoList.get(2 + pageNo*3);
        }

        @Bindable
        public int getRankItemIcon1(){
            return RankIconRids[0 + pageNo*3];
        }
        @Bindable
        public int getRankItemIcon2(){
            return RankIconRids[1 + pageNo*3];
        }
        @Bindable
        public int getRankItemIcon3(){
            return RankIconRids[2 + pageNo*3];
        }

    }

    public class ModuleRankTabViewModel {

        private int tabIndex;

        private ModuleRankTabViewModel(int tabIndex) {
            this.tabIndex = tabIndex;
        }

        public void gotoTabPage() {
            gotoPagePosition(beginPageNo());
        }

        private int beginPageNo() {
            return tabIndex * 2;
        }

        private boolean matchIndex() {
            final int beginPageNo = beginPageNo();
            return (currentItem == beginPageNo || currentItem == (beginPageNo + 1));
        }

        public Typeface getTypeface() {
            return matchIndex() ? Typeface.DEFAULT_BOLD : Typeface.DEFAULT;
        }

        public int getTextSize() {
            return matchIndex() ? 19 : 13;
        }

        public String getTextColor() {
            return matchIndex() ? "#000000" : "#131313";
        }

    }

}
